#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(1)
typedef struct
{
	int date;
	int open;
	int high;
	int low;
	int close;
	int amount;
	int vol;
	int reservation;
} StockData; 

typedef struct
{
	unsigned int date;
	float open;
	float high;
	float low;
	float close;
	float amount;
	float vol;
} StockDayItem; 

typedef struct{
	char ip[42];
	unsigned int date;
	unsigned int time;
}dat_head;

typedef struct{
	char code[24];
	char name[24];
	char dumm[193];
	char sc[9];
}dat_item;

typedef struct{
	char code[23];
	char name[23];
	char dumy[239];
	char sc[29];
}dat_item2;
char end = 0;

StockData stockData;
int read_data(FILE * fp, StockDayItem * &pdata, int & ndata);
int read_tnf(char * file_name);
int read_tnf2(char * file_name);

char  * prefix_tnf = "D:\\hubin\\st\\new_gxzq_v6-1\\T0002\\hq_cache\\";
char *  code_file[]={"shex.tnf","szex.tnf","shm.tnf","szm.tnf"};
int main(int argc,char *argv[])
{
	char file_name[1024];
#if 0
	for(int i=0;i<2;i++ ) {
		strcpy(file_name, prefix_tnf);
		strcat(file_name, code_file[i]);
		read_tnf(file_name);
	}
#else
	for(int i=0;i<2;i++ ) {
		strcpy(file_name, prefix_tnf);
		strcat(file_name, code_file[i+2]);
		read_tnf2(file_name);
	}
#endif
}
int read_tnf(char * file_name)
{
	FILE *fp;
	if((fp = fopen(file_name,"rb")) == NULL)
	{
		printf("Error: Can't open :%s! ",file_name);
		return -1;
	}
	int ndata = 0;

	dat_head head;
	fread(&head, sizeof(dat_head),1, fp);

	dat_item item;
	int i=0;
	while(fread(&item,sizeof(dat_item),1,fp)>0){
		printf("\n %s   %s    %s",item.code, item.name, item.sc);
		i++;
		if(i%100 ==0){
			getch();
		}
	}

	fclose(fp);

	if(getch()==0) getch();
	return 0;
}

int read_tnf2(char * file_name)
{
	FILE *fp;
	if((fp = fopen(file_name,"rb")) == NULL)
	{
		printf("Error: Can't open :%s! ",file_name);
		return -1;
	}
	int ndata = 0;

	dat_head head;
	fread(&head, sizeof(dat_head),1, fp);

	dat_item2 item;
	int i=0;
         	while(fread(&item,sizeof(dat_item2),1,fp)>0){
		printf("\n %s   %s    %s",item.code, item.name, item.sc);
		i++;
		if(i%100 ==0){
			getch();
		}
	}

	fclose(fp);

	if(getch()==0) getch();
	return 0;
}

int read_data(FILE *fp, StockDayItem * &pdata, int & ndata)
{
	float fn;

	fseek(fp,0l,SEEK_END);

	long file_size = ftell(fp);
	ndata = file_size / sizeof(StockData);
	pdata = new StockDayItem[ndata];
	StockDayItem * p = pdata;

	fseek(fp,0l,SEEK_SET);

	while (! feof(fp)) {
		if(fread(&stockData,sizeof(StockData),1,fp)!=1)
			break;
		p->date = stockData.date;
		printf("%10lu ",stockData.date);
		p->open = float(stockData.open)/100;
		printf("%8.2f ",p->open);
		p->high =float(stockData.high)/100;
		printf("%8.2f ",p->high);
		p->low =float(stockData.low)/100;
		printf("%8.2f ",p->low);
		p->close =float(stockData.close)/100;
		printf("%8.2f ",p->close);
		p->amount = stockData.amount;
		printf("%8lu ",p->amount);
		p->vol =float(stockData.vol)/100;
		printf("%8.2f ",p->vol);
		printf("\n");

		p++;
	}
	printf(" ");
	return 0;
}